package SVG::Comic::Character;

use strict;
use warnings;
use Carp;
use vars qw($VERSION $AUTOLOAD);

$VERSION = '0.1.1';

my %fields = (
	character => 'Generic',
	chardata  => undef,
	x         => undef,
	y         => undef,
	z         => 0,
	pan_width => undef,
	pan_height => undef,
	char_width => undef,
);

sub new ($;@) {
	my ($this, %attrs) = @_;
	my $class = ref $this || $this;
	my $self;
	
	foreach my $attr (keys %fields) {
		$attrs{$attr} ||= $fields{$attr};
	}
	
	$self = {
		_permitted => \%fields,
		%attrs,
	};
	
	bless $self, $class;
	
	$self->initialize();
	
	return $self;
}

sub initialize {
	my $self = shift;

}

sub display {
	my $self = shift;
	my $svg  = shift;
	
	my $path = ref($self);
	$path =~ s/::/\//g;
	
	my $character_file;
	my $generic_file;
	
	foreach (sort @INC) {
		my $full_path = $_ . "/" . $path . "/";
		my $generic_path = $full_path . "Generic.scc";
		my $character_path = $full_path . $self->character . ".scc";
		if (-e $generic_path) {
			$generic_file = $generic_path;
		}
		if (-e $character_path) {
			$character_file = $character_path;
			last;
		}
	}
	
	$character_file ||= $generic_file;
	
	print STDERR $character_file, "\n";
	
	open CHARACTER, "<$character_file"
		or die "Cannot open $character_file: $!";
		
	my $chardata = join("", <CHARACTER>);
		
	close CHARACTER;
	
	$self->chardata($chardata) unless $self->chardata;
	
	eval $self->chardata or die "Cannot eval " . $self->chardata . ": $!";
	
}

sub _set_x {
	my $self = shift;
	
	my $x = $self->x + (-1) ** ($self->z/2) * 10 + (1 + (-1) ** ($self->z/2+1)) * ($self->pan_width - $self->char_width) / 2;
	
	if ($self->z/2 % 2) { # if odd...
		$x = $self->pan_width + $self->x - 10;
	} else {
		$x = $self->x + 10
	}
	
	return $x;
}

sub AUTOLOAD {
	my $self = shift; 
	
	my $type = ref($self)
		or croak "$self is not an object.";
	
	my $name =  $AUTOLOAD;
	$name    =~ s/.*://;
	
	unless (exists $self->{_permitted}->{$name}) {
		croak "Can't acces `$name' field in class $type";
	}
	
	if (@_) {
		return $self->{$name} = shift;
	} else {
		return $self->{$name};
	}
}

sub DESTROY {
	my $self = shift;
}

1;